package com.pig4cloud.mall.product.controller;

import com.pig4cloud.mall.product.service.ProductAttributeCategoryService;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 商品属性分类(类型)
 *
 * @author fangyu
 * @date 2020-12-26 23:41:08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/attr_category")
@Api(value = "attr_category", tags = "商品类型管理")
public class ProductAttributeCategoryController {

	private final ProductAttributeCategoryService attrCategoryService;

	@ApiOperation(value = "分页获取商品类型", notes = "分页获取商品类型")
	@GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('product_attrgroup_get')")
	public R getList(
			@RequestParam(value = "current", defaultValue = "1") Integer current,
			@RequestParam(value = "size", defaultValue = "10") Integer size
	) {
		return R.ok(attrCategoryService.listAttrCategory(current, size));
	}

	/**
	 * 添加商品类型
	 *
	 * @param name 商品类型名称
	 * @return R
	 */
	@ApiOperation(value = "添加商品类型", notes = "添加商品类型")
	@SysLog("添加商品类型")
	@PostMapping("/create")
	@PreAuthorize("@pms.hasPermission('product_attrgroup_add')")
	public R create(@RequestParam String name) {
		int count = attrCategoryService.create(name);
		if (count > 0) {
			return R.ok();
		} else {
			return R.failed("添加失败！");
		}
	}

	/**
	 * 删除商品类型
	 *
	 * @param id 商品类型id
	 * @return R
	 */
	@ApiOperation(value = "添加商品类型", notes = "添加商品类型")
	@SysLog("添加商品类型")
	@PostMapping("/delete/{id}")
	@PreAuthorize("@pms.hasPermission('product_attrgroup_del')")
	public R delete(@PathVariable Long id) {
		return attrCategoryService.delete(id);

	}

}
